<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app"></div>

  <script type="text/html" id="box">
    <h2>我是{{username}}, 今年{{age}}了, {{ qwer }}</h2>
  </script>

  <script>
    function template(el, val) {
      let str = document.getElementById(el).innerHTML
      let reg = /{{\s*([a-zA-Z]+)\s*}}/
      let arr

      while(arr = reg.exec(str)) {
        // 字符串的所有方法 不会修改字符串本身
        str = str.replace(arr[0], val[arr[1]])  
      }

      return str
    }

    let data = {
      username: '张三',
      age: 18,
      qwer: '你真好'
    }

    let htmlStr = template('box', data)

    document.querySelector('#app').innerHTML = htmlStr
  </script>
</body>
</html>